#!/usr/bin/python
# -*- coding: UTF-8 -*-
# from Common.stringManage import *
import time
import json
import os
import sys
import shutil
import string
uselessfile = ('CommonTemplate',)
def containVarInString(containVar,stringVar):
        if isinstance(stringVar, str):
            if  stringVar.find(containVar) > -1:
                return True
            else:
                return False
        else:
            return False
def listAllDir(sourceDir):
    result = []
    if not os.path.exists(sourceDir):
        print("源目录不存在")
    print(os.listdir(sourceDir))
    for filename in os.listdir(sourceDir):
        if os.path.isdir(sourceDir+"/"+filename):
            result.append(filename)
    return result
# 复制目录到指定路径
def listDir(sourceDir):
    result = {}
    #return{}
    if not os.path.exists(sourceDir):
        print("源目录不存在")
    for filename in os.listdir(sourceDir):
        filePath = sourceDir+"/"+filename
        if os.path.isdir(filePath):
            # print(filename)
            if containVarInString("Template",filename):
                # print(filename)
                for item in uselessfile:
                    # print(item)
                    if  item != filename:
                        modulePath="Template."+filename+"."+filename
                        module=__import__(modulePath)
                        firstsubModule=getattr(module, filename)
                        secondsubModule=getattr(firstsubModule, filename)
                        templateName=getattr(secondsubModule, filename)
                        #print(templateName.TemplateName)
                        # firstsubModule=getattr(module, templetePath)
                        # secondsubModule=getattr(firstsubModule, templetePath)
                        # templateName=getattr(secondsubModule, templetePath)
                        #print(module)
                        # length=len("Template")
                        # pos = filename.find("Template")
                        # resp = replaceStr(pos,filename,length,"")
                        result[filename] = templateName.TemplateName
    return result
#替换字符 
def replaceStr(pos,content,length,replaceStr): 
    if pos != -1:
        return content[:pos] +replaceStr +content[pos+length:]

def findFile(path,despathName):
        result = []
        for filename in os.listdir(path):
            filePath =  path+"/"+filename
            if os.path.isdir(filePath) and filename==despathName:
                for filename2 in os.listdir(filePath):
                    filePath2 =  filePath+"/"+filename2
                    if os.path.isdir(filePath2):
                        shutil.rmtree(filePath2)
                    else:
                        if not ".DS_Store" in filename2:
                            result.append(filePath2)
            else:
                pass
        return result
# print(listDir("/Users/yanhe/Desktop/coding/workspace/py封装/Template"))
